/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package brickroadfitness;


import java.util.Vector;
import java.sql.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;


public class EquipInfo extends JFrame implements ActionListener {

    public Statement stmt1;
    public PreparedStatement pstmt;
    public ResultSet rset;
    public String query1;
    private JLabel ID;
    private JLabel IDlabel;
    private JButton back;
    private JButton delete;
    private JButton edit;
    private JLabel equipName;
    private JLabel equipPrice;
    private JLabel equipinfo;
    private JLabel header;
    private JPanel infopanel;
    private JLabel nameLabel;
    private JLabel priceLabel;
    private JLabel condition;
    private JLabel statlabel;
    private JOptionPane delequip;
    private JRadioButton func, broken;
    private JRadioButton used, notused;
    private String equipID, key;
    private int index = -1;


    public EquipInfo()
    {
        super("Brickroad Fitness - Equipment Information");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBackground(new Color(255, 255, 102));
        setResizable(false);
        setVisible(true);
        //setLocationRelativeTo(null);
        setForeground(new Color(255, 255, 102));
        initComponents();
    }

    public EquipInfo(String id)
    {
        super("Brickroad Fitness - Equipment Information");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBackground(new Color(255, 255, 102));
        setResizable(false);
        setVisible(true);
        //setLocationRelativeTo(null);
        setForeground(new Color(255, 255, 102));
        equipID = id;
        initComponents();
    }

    public EquipInfo(String id, int index, String key)
    {
        super("Brickroad Fitness - Equipment Information");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBackground(new Color(255, 255, 102));
        setResizable(false);
        setVisible(true);
        //setLocationRelativeTo(null);
        setForeground(new Color(255, 255, 102));
        this.index = index;
        this.key = key;
        equipID = id;
        initComponents();
    }

    private void initComponents() {

        header = new JLabel();
        infopanel = new JPanel();
        equipinfo = new JLabel();
        IDlabel = new JLabel();
        nameLabel = new JLabel();
        priceLabel = new JLabel();
        condition = new JLabel();
        statlabel = new JLabel();
        ID = new JLabel();
        equipName = new JLabel();
        equipPrice = new JLabel();
        edit = new JButton();
        delete = new JButton();
        back = new JButton();
        func = new JRadioButton();
        broken = new JRadioButton();
        used = new JRadioButton();
        notused = new JRadioButton();
        ButtonGroup statgrp = new ButtonGroup();
        ButtonGroup radiogrp = new ButtonGroup();

        statgrp.add(used);
        statgrp.add(notused);
        radiogrp.add(func);
        radiogrp.add(broken);

        back.addActionListener(this);
        edit.addActionListener(this);
        delete.addActionListener(this);

        header.setIcon(new ImageIcon(getClass().getResource("/PRIMER.jpg"))); // NOI18N
        header.setText("Brickroad Fitness");

        infopanel.setBackground(new Color(255, 255, 102));
        infopanel.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 5));

        equipinfo.setFont(new Font("Arial", 3, 18));
        equipinfo.setText("Equipment Information");

        IDlabel.setFont(new Font("Tempus Sans ITC", 1, 16));
        IDlabel.setText("Equipment ID: ");

        nameLabel.setFont(new Font("Tempus Sans ITC", 1, 16));
        nameLabel.setText("Equipment Name:");

        priceLabel.setFont(new Font("Tempus Sans ITC", 1, 16)); // NOI18N
        priceLabel.setText("Price: ");

        condition.setFont(new Font("Tempus Sans ITC", 1, 16)); // NOI18N
        condition.setText("Condition:");

        statlabel.setFont(new Font("Tempus Sans ITC", 1, 16)); // NOI18N
        statlabel.setText("Status:");

        ID.setFont(new Font("Tahoma", 1, 14));
       // ID.setText("1");

        equipName.setFont(new Font("Tahoma", 1, 14));
        //equipName.setText("Proteus MTM 7520 Motorized Treadmill ");

        equipPrice.setFont(new Font("Tahoma", 1, 14)); // NOI18N
        //equipPrice.setText("75 500");

        func.setBackground(new Color(255, 255, 102));
        func.setFont(new Font("Tahoma", 1, 14)); // NOI18N
        func.setText("Functional");
        func.setEnabled(false);

        broken.setBackground(new Color(255, 255, 102));
        broken.setFont(new Font("Tahoma", 1, 14)); // NOI18N
        broken.setText("Broken");
        broken.setEnabled(false);
        //broken.setSelected(true);

        used.setBackground(new Color(255, 255, 102));
        used.setFont(new Font("Tahoma", 1, 14)); // NOI18N
        used.setText("Active");
        used.setEnabled(false);

        notused.setBackground(new Color(255, 255, 102));
        notused.setFont(new Font("Tahoma", 1, 14)); // NOI18N
        notused.setText("Inactive");
        notused.setEnabled(false);
        notused.setSelected(true);

        cnd a = new cnd();

        try {
            a.connect();
            pstmt = a.con1.prepareStatement("SELECT * FROM `equipment` where `eqID` = ?");
            pstmt.setInt(1, Integer.parseInt(equipID));
            rset = pstmt.executeQuery();
            rset.next();

            ID.setText(rset.getString("eqID"));
            equipName.setText(rset.getString("eqName"));
            equipPrice.setText(rset.getString("eqPrice"));

            if(rset.getString("eqStatus").equals("Active"))
                used.setSelected(true);
            else
                notused.setSelected(true);
            
            if(rset.getString("eqCondition").equals("Functional"))
                func.setSelected(true);
            else
                broken.setSelected(true);
            a.disconnect();
        }
        catch (Exception ex)
        {
            JOptionPane invalidsearch = new JOptionPane();
            invalidsearch.showMessageDialog(infopanel, "Error copying records from equipment table. " + ex.getMessage());
            System.out.println("Error copying records from equipment table. " + ex.getMessage());
        }
        
        edit.setFont(new Font("Century Gothic", 1, 16)); // NOI18N
        edit.setText("EDIT");
        edit.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        delete.setFont(new Font("Century Gothic", 1, 16)); // NOI18N
        delete.setText("DEACTIVATE");
        delete.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        back.setFont(new Font("Century Gothic", 1, 16)); // NOI18N
        back.setText("BACK");
        back.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        GroupLayout infopanelLayout = new GroupLayout(infopanel);
        infopanel.setLayout(infopanelLayout);
        infopanelLayout.setHorizontalGroup(
            infopanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(GroupLayout.Alignment.TRAILING, infopanelLayout.createSequentialGroup()
                .addGroup(infopanelLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addGroup(GroupLayout.Alignment.LEADING, infopanelLayout.createSequentialGroup()
                        .addGap(133, 133, 133)
                        .addGroup(infopanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(IDlabel)
                            .addComponent(nameLabel)
                            .addComponent(priceLabel)
                            .addComponent(condition)
                            .addComponent(statlabel))
                        .addGroup(infopanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addGroup(infopanelLayout.createSequentialGroup()
                                .addGap(56, 56, 56)
                                .addGroup(infopanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                    .addComponent(equipName)
                                    .addComponent(equipPrice)
                                    .addComponent(ID)))
                            .addGroup(infopanelLayout.createSequentialGroup()
                                .addGap(48, 48, 48)
                                .addComponent(func)
                                .addGap(18, 18, 18)
                                .addComponent(broken))
                            .addGroup(infopanelLayout.createSequentialGroup()
                                .addGap(48, 48, 48)
                                .addComponent(used)
                                .addGap(18, 18, 18)
                                .addComponent(notused)))
                        .addGroup(infopanelLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                            .addGroup(infopanelLayout.createSequentialGroup()
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 169, Short.MAX_VALUE)
                                .addGroup(infopanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                    .addComponent(delete, GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                                    .addComponent(edit, GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)))
                            .addGroup(infopanelLayout.createSequentialGroup()
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(back, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE))))
                    .addGroup(GroupLayout.Alignment.LEADING, infopanelLayout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(equipinfo)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 644, Short.MAX_VALUE)))
                .addGap(55, 55, 55))
        );
        infopanelLayout.setVerticalGroup(
            infopanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(infopanelLayout.createSequentialGroup()
                .addGap(56, 56, 56)
                .addComponent(equipinfo)
                .addGap(32, 32, 32)
                .addGroup(infopanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(infopanelLayout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(edit, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(delete, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(back, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
                    .addGroup(infopanelLayout.createSequentialGroup()
                        .addComponent(ID, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(equipName))
                    .addGroup(infopanelLayout.createSequentialGroup()
                        .addComponent(IDlabel)
                        .addGap(18, 18, 18)
                        .addComponent(nameLabel)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(infopanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(priceLabel)
                            .addComponent(equipPrice))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(infopanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(condition)
                            .addComponent(func)
                            .addComponent(broken))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(infopanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(statlabel)
                            .addComponent(used)
                            .addComponent(notused))))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(header, GroupLayout.PREFERRED_SIZE, 937, Short.MAX_VALUE)
            .addComponent(infopanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(header)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(infopanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }

    public void actionPerformed(ActionEvent e)
    {
        Object event = new Object();
        event = e.getSource();

        if (event == back)
        {
            dispose();
            if (index == -1)
                new ViewEquip();
            else
                new SearchEquip(index, key);
        }
        else if (event == delete)
        {
            int ans;
            ans = delequip.showConfirmDialog(infopanel, "Are you sure you want to delete this equipment?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            if (ans == 0)
            {
                JOptionPane invalidsearch = new JOptionPane();
                cnd a = new cnd();
                try {
                a.connect();
                stmt1 = a.con1.createStatement();
                query1 = "UPDATE `equipment`" +
                        "SET `eqStatus` = \"" + "Inactive"+
                        "\" WHERE `eqID` = " + equipID;
                            stmt1.executeUpdate(query1);
                         System.out.println("Edit Successful");
                         invalidsearch.showMessageDialog(this, "Equipment deactivated.");
                         dispose();
                         new Main();
                }

                catch (Exception ex)
                {
                    invalidsearch.showMessageDialog(infopanel, "Error copying records from equipment table. " + ex.getMessage());
                    System.out.println("Error copying records from equipment table. " + ex.getMessage());
                }
            }
        }
        else if (event == edit)
        {
            dispose();
            new EditEquip(equipID);
        }
    }
}

